<?php
class Options_Framework_Admin {

    protected $options_screen = null;
    public function init() {
    	$options = & Options_Framework::_optionsframework_options();
    	if ( $options ) {
			add_action( 'admin_menu', array( $this, 'add_custom_options_page' ) );
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
			add_action( 'admin_init', array( $this, 'settings_init' ) );
			add_action( 'wp_before_admin_bar_render', array( $this, 'optionsframework_admin_bar' ) );
		}
    }

    function settings_init() {
		$options_framework = new Options_Framework;
	    $name = $options_framework->get_option_name();
		register_setting( 'optionsframework', $name, array ( $this, 'validate_options' ) );
		add_action( 'optionsframework_after_validate', array( $this, 'save_options_notice' ) );
    }


	static function menu_settings() {
		$menu = array(
            'mode' => 'submenu',
            'page_title' => __( '主题选项', 'theme-textdomain' ),
			'menu_title' => __( '主题选项', 'theme-textdomain' ),
			'capability' => 'edit_theme_options',
			'menu_slug' => 'ds_setting',
            'parent_slug' => 'themes.php',
            'icon_url' => 'dashicons-admin-generic',
            'position' => '61'
		);
		return apply_filters( 'optionsframework_menu', $menu );
	}

	function add_custom_options_page() {
		$menu = $this->menu_settings();
		$this->options_screen = add_theme_page(
            $menu['page_title'],
            $menu['menu_title'],
            $menu['capability'],
            $menu['menu_slug'],
            array( $this, 'options_page' )
        );
	}

	function enqueue_admin_styles( $hook ) {
		if ( $this->options_screen != $hook )
	        return;
		wp_enqueue_style( 'optionsframework', OPTIONS_FRAMEWORK_DIRECTORY . 'css/optionsframework.css', array(),  Options_Framework::VERSION );
		wp_enqueue_style( 'wp-color-picker' );
	}

	function enqueue_admin_scripts( $hook ) {

		if ( $this->options_screen != $hook )
	        return;
		wp_enqueue_script(
			'options-custom',
			OPTIONS_FRAMEWORK_DIRECTORY . 'js/options-custom.js',
			array( 'jquery','wp-color-picker' ),
			Options_Framework::VERSION
		);
		add_action( 'admin_head', array( $this, 'of_admin_head' ) );
	}
	function of_admin_head() {
		do_action( 'optionsframework_custom_scripts' );
	}

	 function options_page() { ?>

	<div id="optionsframework-wrap" class="wrap dss">
		<?php $menu = $this->menu_settings(); ?>
		<link rel="stylesheet" type="text/css" href="<?php bloginfo('template_directory'); ?>/setting/checkbox/Checkbox.css" />
		<script src="<?php bloginfo('template_directory'); ?>/setting/checkbox/Checkbox.js"></script>
		
		<div class="wave-box">
			<div id="marquee-box" class="marquee-box">
				<div class="marquee">
					<div id="wave-list-box1" class="wave-list-box">
						<ul><li><img alt="波浪" src="<?php bloginfo('template_directory'); ?>/setting/img/wave_02.png" height="86"></li></ul>
					</div>
					<div id="wave-list-box2" class="wave-list-box"></div>
				</div>
			</div>
			<div id="marquee-box3" class="marquee-box">
				<div class="marquee">
					<div id="wave-list-box4" class="wave-list-box">
						<ul><li><img alt="波浪" src="<?php bloginfo('template_directory'); ?>/setting/img/wave_01.png" height="86"></li></ul>
					</div>
				<div id="wave-list-box5" class="wave-list-box"></div></div>
			</div>
			<div class="szdbt">
				<b><?php echo esc_html( $menu['page_title'] ); ?></b>
				<span style="font-size:12px;margin-left:20px;">主题版本 v1.0</span>
				<a href="http://ztmao.com/go/qqmail" target="_blank" class="mailico"><i class="dashicons dashicons-email"></i></a>
				<a href="http://ztmao.com/go/kefu" target="_blank" class="qqico"><i class="dashicons dashicons-admin-users"></i></a>
				<div class="fldid tooltips"><i class="dashicons dashicons-editor-alignleft"></i><?php exampletheme_options_after(); ?></div>
			</div>
		</div>
		
		<div class="dssbox">
			<div class="nav-tab-wrapper">
				<div class="htdzcss">
					<h4>去作者那找找教程？</h4>
					<form class="ssdwkz" name="NewsSearchForm" method="get" action="http://ztmao.com" target="_blank">
						<input type="text" name="s" value="输入问题关键字" class="txt" id="seachkeywords" onfocus="if(this.value=='输入问题关键字'){this.value=''}" onblur="if(this.value=='')this.value='输入问题关键字'">
						<input type="text" id="flycc" name="cat" value="1" style="display:none;">
						<button type="submit" class="s_ss"><i class="dashicons dashicons-search"></i></button>
					</form>
				</div>
				<div class="szdtabk">
					<?php echo Options_Framework_Interface::optionsframework_tabs(); ?>
				</div>
			</div>
			<?php settings_errors( 'options-framework' ); ?>
			<div id="optionsframework-metabox" class="metabox-holder">
				<div id="optionsframework" class="postbox">
					<form action="options.php" method="post">
					<?php settings_fields( 'optionsframework' ); ?>
					<?php Options_Framework_Interface::optionsframework_fields(); /* Settings */ ?>
					<div id="optionsframework-submit">
						<input type="submit" class="button-primary" name="update" value="<?php esc_attr_e( '保存设置', 'theme-textdomain' ); ?>" />
						<input type="submit" class="reset-button button-secondary" name="reset" value="<?php esc_attr_e( '重置所有设置', 'theme-textdomain' ); ?>" onclick="return confirm( '<?php print esc_js( __( '确定清空所有设置参数？', 'theme-textdomain' ) ); ?>' );" />
						<div class="clear"></div>
					</div>
					</form>
				</div> <!-- / #container -->
			</div>
		</div>
		<?php do_action( 'optionsframework_after' ); ?>
	</div> <!-- / .wrap -->

	<?php
	}
	function validate_options( $input ) {
		if ( isset( $_POST['reset'] ) ) {
			add_settings_error( 'options-framework', 'restore_defaults', __( '已恢复为默认参数', 'theme-textdomain' ), 'updated fade' );
			return $this->get_default_values();
		}
		$clean = array();
		$options = & Options_Framework::_optionsframework_options();
		foreach ( $options as $option ) {
			if ( ! isset( $option['id'] ) ) {
				continue;
			}
			if ( ! isset( $option['type'] ) ) {
				continue;
			}
			$id = preg_replace( '/[^a-zA-Z0-9._\-]/', '', strtolower( $option['id'] ) );
			if ( 'checkbox' == $option['type'] && ! isset( $input[$id] ) ) {
				$input[$id] = false;
			}
			if ( 'multicheck' == $option['type'] && ! isset( $input[$id] ) ) {
				foreach ( $option['options'] as $key => $value ) {
					$input[$id][$key] = false;
				}
			}
			if ( has_filter( 'of_sanitize_' . $option['type'] ) ) {
				$clean[$id] = apply_filters( 'of_sanitize_' . $option['type'], $input[$id], $option );
			}
		}
		do_action( 'optionsframework_after_validate', $clean );
		return $clean;
	}
	function save_options_notice() {
		add_settings_error( 'options-framework', 'save_options', __( '保存设置', 'theme-textdomain' ), 'updated fade' );
	}
	function get_default_values() {
		$output = array();
		$config = & Options_Framework::_optionsframework_options();
		foreach ( (array) $config as $option ) {
			if ( ! isset( $option['id'] ) ) {
				continue;
			}
			if ( ! isset( $option['std'] ) ) {
				continue;
			}
			if ( ! isset( $option['type'] ) ) {
				continue;
			}
			if ( has_filter( 'of_sanitize_' . $option['type'] ) ) {
				$output[$option['id']] = apply_filters( 'of_sanitize_' . $option['type'], $option['std'], $option );
			}
		}
		return $output;
	}
	
	function optionsframework_admin_bar() {

		$menu = $this->menu_settings();

		global $wp_admin_bar;

		if ( 'menu' == $menu['mode'] ) {
			$href = admin_url( 'admin.php?page=' . $menu['menu_slug'] );
		} else {
			$href = admin_url( 'themes.php?page=' . $menu['menu_slug'] );
		}
		$args = array(
			'parent' => 'appearance',
			'id' => 'of_theme_options',
			'title' => $menu['menu_title'],
			'href' => $href
		);
		$wp_admin_bar->add_menu( apply_filters( 'optionsframework_admin_bar', $args ) );
	}
}